﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace HenryNguyen.GNCMedia.ESchool.WebUI.UserControls
{
    public partial class ContactsList : System.Web.UI.UserControl
    {
        protected void Page_Init(object sender, EventArgs e)
        { ucDataNavigator.CurrentPage = 1; }

        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!Page.IsPostBack &&
            //    Visible)
            //{
            //    ucConfirmDialog.Hide();
            //    FillContactGroups();
            //    FillContacts(1);
            //}
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            FillContacts(1);
        }

        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (Repeater1.Items.Count == 0)
            {
                if (e.Item.ItemType == ListItemType.Footer)
                {
                    Label lblFooter = e.Item.FindControl("lblFooter") as Label;
                    lblFooter.Text = "<tr>" +
                        "<td colspan='4' style='tex-align:center;font-weight:bold'>Không có dữ liệu</td>" +
                        "</tr>";
                }
            }
        }

        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName.ToLower().Equals("update")) { Response.Redirect("~/Contacts.aspx?Module=Update&ID=" + Convert.ToInt32(e.CommandArgument)); }
            if (e.CommandName.ToLower().Equals("delete"))
            {
                ucConfirmDialog.MessageContent = "Bạn có chắc chắn muốn xóa bản ghi này?";
                ucConfirmDialog.Key = e.CommandArgument;
                ucConfirmDialog.Show();
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Contacts.aspx?Module=Add");
        }

        protected void btnImport_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Contacts.aspx?Module=Import");
        }

        private void FillContactGroups()
        {
        //    ddlContactGroups.Items.Clear();
        //    ddlContactGroups.Items.Add(new ListItem("Tất cả", "0"));

        //    ContactGroupsBL vContactGroupsBL = new ContactGroupsBL();
        //    List<ContactGroupsDO> vContactGroupsDOCollection = null;

        //    try
        //    {
        //        vContactGroupsDOCollection = vContactGroupsBL.SelectAll(Convert.ToInt32(Session["GNCMedia.ESchool.CustomerID"]), Convert.ToInt32(Session["GNCMedia.ESchool.SchoolID"]));

        //        if (vContactGroupsDOCollection != null)
        //        {
        //            if (vContactGroupsDOCollection.Count > 0)
        //            {
        //                for (int i = 0; i < vContactGroupsDOCollection.Count; i++)
        //                { ddlContactGroups.Items.Add(new ListItem(vContactGroupsDOCollection[i].GroupName, vContactGroupsDOCollection[i].GroupID.ToString())); }
        //            }
        //        }
        //    }
        //    catch (Exception ex) { Response.Write(ex.Message); }
        //    finally
        //    {
        //        vContactGroupsBL.Dispose();
        //        vContactGroupsBL = null;
        //        vContactGroupsDOCollection = null;
        //    }
        }

        private void FillContacts(int pageIndex)
        {
            //int groupId = Convert.ToInt32(ddlContactGroups.SelectedItem.Value);
            //int pageSize = Convert.ToInt32(ConfigurationManager.AppSettings["PageSize"]);
            //int rowCount = 0;
            //ContactsBL vContactsBL = new ContactsBL();
            //ContactsDO vContactsDO = new ContactsDO();
            //vContactsDO.FullName = txtFullName.Text.Trim();
            //vContactsDO.PhoneNumber = txtFullName.Text.Trim();
            //vContactsDO.CustomerID = Convert.ToInt32(Session["GNCMedia.ESchool.CustomerID"]);
            //vContactsDO.SchoolID = Convert.ToInt32(Session["GNCMedia.ESchool.SchoolID"]);

            //try
            //{
            //    Repeater1.DataSource = vContactsBL.SelectAll(vContactsDO, groupId, pageIndex, pageSize, ref rowCount);
            //    PreparePaging(pageIndex, pageSize, rowCount);
            //    Repeater1.DataBind();
            //}
            //catch (Exception ex) { Response.Write(ex.Message); }
            //finally
            //{
            //    vContactsBL.Dispose();
            //    vContactsBL = null;
            //    vContactsDO = null;
            //}
        }

        #region Navigator Button Controller

        private void PreparePaging(int pageIndex, int pageSize, int rowCount)
        {
            int numOfPage;

            lblTotal.Text = rowCount.ToString("#,###");

            if (rowCount == 0)
            {
                numOfPage = 0;
                lblTotal.Text = "0";
                ucDataNavigator.Visible = false;
            }
            else if (rowCount <= pageSize)
            {
                numOfPage = 1;
                ucDataNavigator.Visible = false;
            }
            else
            {
                ucDataNavigator.Visible = true;

                if (rowCount % pageSize == 0)
                {
                    numOfPage = rowCount / pageSize;
                }
                else
                {
                    numOfPage = rowCount / pageSize + 1;
                }
            }

            //Khởi tạo lại PageController
            if (numOfPage == 0)
                ucDataNavigator.RefreshControl(0, numOfPage);
            else if (pageIndex <= numOfPage)
                ucDataNavigator.RefreshControl(pageIndex, numOfPage);
            else
                ucDataNavigator.RefreshControl(1, numOfPage);
            //Thiết lập Enable và Disable các nút di chuyển trang
            EnableDisableButtons(numOfPage);

        }

        //protected void PageChanged(object sender, DataNavigatorEventArgs e)
        //{
        //    ucDataNavigator.CurrentPage = e.CurrentPage;
        //    FillContacts(ucDataNavigator.CurrentPage);
        //    EnableDisableButtons(e.TotalPages);
        //}

        //protected void FirstPage(object sender, DataNavigatorEventArgs e)
        //{
        //    // Decrement the current page index.
        //    if (e.CurrentPage > 1)
        //    {
        //        ucDataNavigator.CurrentPage = 1;
        //        // Get the data for the DataGrid.
        //        FillContacts(ucDataNavigator.CurrentPage);
        //        EnableDisableButtons(e.TotalPages);
        //    }
        //}

        //protected void PreviousPage(object sender, DataNavigatorEventArgs e)
        //{
        //    // Decrement the current page index.
        //    if (e.CurrentPage > 1)
        //    {
        //        ucDataNavigator.CurrentPage--;
        //        // Get the data for the DataGrid.
        //        FillContacts(ucDataNavigator.CurrentPage);
        //        EnableDisableButtons(e.TotalPages);
        //    }
        //}

        //protected void NextPage(object sender, DataNavigatorEventArgs e)
        //{
        //    // Decrement the current page index.
        //    if (e.CurrentPage <= e.TotalPages)
        //    {
        //        ucDataNavigator.CurrentPage++;
        //        // Get the data for the DataGrid.
        //        FillContacts(ucDataNavigator.CurrentPage);
        //        EnableDisableButtons(e.TotalPages);
        //    }
        //}

        //protected void LastPage(object sender, DataNavigatorEventArgs e)
        //{
        //    // Decrement the current page index.
        //    if (e.CurrentPage <= e.TotalPages)
        //    {
        //        ucDataNavigator.CurrentPage = e.TotalPages;
        //        // Get the data for the DataGrid.
        //        FillContacts(ucDataNavigator.CurrentPage);
        //        EnableDisableButtons(e.TotalPages);
        //    }
        //}

        private void EnableDisableButtons(int iTotalPages)
        {
            // Should we disable the first link?
            ucDataNavigator.FirstButtonEnabled = (ucDataNavigator.CurrentPage == 1) || (ucDataNavigator.CurrentPage == 0) ? false : true;
            ucDataNavigator.FirstButtonImageUrl = (ucDataNavigator.CurrentPage == 1) || (ucDataNavigator.CurrentPage == 0) ? "~/images/sq_br_first_disable.png" : "~/images/sq_br_first.png";
            // Should we disable the previous link?
            ucDataNavigator.PreviousButtonEnabled = (ucDataNavigator.CurrentPage == 1) || (ucDataNavigator.CurrentPage == 0) ? false : true;
            ucDataNavigator.PreviousButtonImageUrl = (ucDataNavigator.CurrentPage == 1) || (ucDataNavigator.CurrentPage == 0) ? "~/images/sq_br_prev_disable.png" : "~/images/sq_br_prev.png";
            // Should we enable the next link?
            ucDataNavigator.NextButtonEnabled = (ucDataNavigator.CurrentPage < iTotalPages) ? true : false;
            ucDataNavigator.NextButtonImageUrl = (ucDataNavigator.CurrentPage < iTotalPages) ? "~/images/sq_br_next.png" : "~/images/sq_br_next_disable.png";
            // Should we enable the last link?
            ucDataNavigator.LastButtonEnabled = (ucDataNavigator.CurrentPage < iTotalPages) ? true : false;
            ucDataNavigator.LastButtonImageUrl = (ucDataNavigator.CurrentPage < iTotalPages) ? "~/images/sq_br_last.png" : "~/images/sq_br_last_disable.png";
        }

        override protected void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent()
        {
            //this.lstdivUserRef.SelectedIndexChanged += new EventHandler(lstdivUserRef_SelectedIndexChanged);
            //this.ucDataNavigator.FirstPage += new FirstPageEventHandler(this.FirstPage);
            //this.ucDataNavigator.PreviousPage += new PreviousPageEventHandler(this.PreviousPage);
            //this.ucDataNavigator.NextPage += new NextPageEventHandler(this.NextPage);
            //this.ucDataNavigator.LastPage += new LastPageEventHandler(this.LastPage);
            //this.ucDataNavigator.PageChanged += new PageChangedEventHandler(this.PageChanged);
            //this.Init += new EventHandler(this.Page_Init);

            //ucConfirmDialog.Yes += new YesEventHandler(ucConfirmDialog_Yes);
            //ucConfirmDialog.No += new NoEventHandler(ucConfirmDialog_No);
        }

        #endregion

        #region Confirm Dialog Button Controller
        protected void ucConfirmDialog_Yes(object sender)
        {
            //ucConfirmDialog.Hide();
            //ContactsBL vContactsBL = new ContactsBL();

            //try
            //{
            //    int result = vContactsBL.Delete(Convert.ToInt32(ucConfirmDialog.Key), Convert.ToInt32(Session["GNCMedia.Edu.EContact.CustomerID"]));

            //    if (result > 0)
            //    {
            //        ucNotificationDialog.MessageContent = "Bản ghi đã được xóa thành công!";
            //        ucNotificationDialog.MessageType = 1;
            //        ucNotificationDialog.Show();
            //    }
            //    else
            //    {
            //        if (result == -1) { ucNotificationDialog.MessageContent = "Bản ghi không tồn tại!<br/>Vui lòng kiểm tra lại."; }
            //        else { ucNotificationDialog.MessageContent = "Hệ thống gặp sự cố!<br/>Vui lòng liên hệ GNCMedia để được hỗ trợ."; }

            //        ucNotificationDialog.MessageType = 0;
            //        ucNotificationDialog.Show();
            //    }
            //}
            //catch (Exception ex)
            //{
            //    Response.Write(ex.Message);
            //    ucNotificationDialog.MessageContent = "Hệ thống gặp sự cố!<br/>Vui lòng liên hệ GNCMedia để được hỗ trợ.";
            //    ucNotificationDialog.MessageType = 0;
            //    ucNotificationDialog.Show();
            //}
            //finally
            //{
            //    vContactsBL.Dispose();
            //    vContactsBL = null;
            //}
        }

        protected void ucConfirmDialog_No(object sender) { ucConfirmDialog.Hide(); }
        #endregion
    }
}